package mo.leetcode_top;

import mo.common.ListNode;


public class L21 {

    public ListNode mergeTwoLists(ListNode list1, ListNode list2) {
        ListNode sol = new ListNode(-1);
        ListNode current = sol;
        while (list1 != null && list2 != null) {
            while (list1 != null && list1.val <= list2.val) {
                current.next = new ListNode(list1.val);
                current = current.next;
                list1 = list1.next;
            }
            while (list1 != null && list2 != null && list2.val <= list1.val) {
                current.next = new ListNode(list2.val);
                current = current.next;
                list2 = list2.next;
            }
        }
        while (list1 != null) {
            current.next = new ListNode(list1.val);
            current = current.next;
            list1 = list1.next;
        }
        while (list2 != null) {
            current.next = new ListNode(list2.val);
            current = current.next;
            list2 = list2.next;
        }
        return sol.next;
    }

    public static void main(String[] args) {
        L21 test = new L21();
        ListNode sol = test.mergeTwoLists(null, null);
        if (sol != null) {
            sol.print();
        }
    }
}
